package com.sunshine.lnuplus.view.Timetable;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProviders;

import android.content.Intent;
import android.database.DatabaseUtils;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sunshine.lnuplus.R;
import com.sunshine.lnuplus.databinding.ItemDialogLessonDetailBinding;
import com.sunshine.lnuplus.utils.LessonDetailHelper;
import com.sunshine.lnuplus.utils.TagHelper;
import com.sunshine.lnuplus.view.AddLesson.AddLessonActivity;

import java.util.List;

public class LessonDetailFragment extends Fragment {

    private ItemDialogLessonDetailBinding binding;
    private static LessonDetailHelper lessonDetailHelper;

    public static LessonDetailFragment newInstance(String lesson, String time, LessonDetailHelper lessonDetailHelper1) {
        LessonDetailFragment lessonDetailFragment = new LessonDetailFragment();
        lessonDetailHelper = lessonDetailHelper1;
        Bundle bundle = new Bundle();
        bundle.putString("lesson",lesson);
        bundle.putString("time",time);
        lessonDetailFragment.setArguments(bundle);
        return lessonDetailFragment;
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater,R.layout.item_dialog_lesson_detail,container,false);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String lessonName = null,teacher = null,room = null,time = null,color = null, weeks = null;

        for(List<String> list : TimetableFragment.getTimetable()){
            if(list.get(0).equals(getArguments().getString("lesson"))&&list.get(3).equals(getArguments().getString("time"))){
                lessonName = list.get(0);
                teacher = list.get(1);
                room = list.get(2);
                time = list.get(3);
                color = list.get(4);
                weeks = list.get(5);
            }
        }
        binding.detailLessonName.setText("课程名称："+lessonName);
        binding.detailTeacher.setText("教师："+teacher);
        binding.detailRoom.setText("教室："+room);
        binding.detailTime.setText("上课时间："+time);
        binding.detailWeeks.setText("上课周数："+weeks);

        final String finalLessonName = lessonName;
        final String finalTeacher = teacher;
        final String finalRoom = room;
        final String finalTime = time;
        final String finalColor = color;
        final String finalWeeks = weeks;
        binding.detailEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("lesson_name", finalLessonName);
                bundle.putString("teacher", finalTeacher);
                bundle.putString("room", finalRoom);
                bundle.putString("time", finalTime);
                bundle.putString("color", finalColor);
                bundle.putString("weeks", finalWeeks);
                bundle.putBoolean("add",false);
                startActivityForResult(new Intent(getContext(), AddLessonActivity.class).putExtras(bundle), TagHelper.LESSONDETAIL);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode==TagHelper.LESSONDETAIL&&resultCode==TagHelper.ADDLESSON){
            lessonDetailHelper.callBack();
        }
    }
}
